//
//  RegistFirstVC.m
//  YangWifi
//
//  Created by zuoxueliangduyuanzhuo on 2017/11/13.
//  Copyright © 2017年 zuoxueliangduyuanzhuo. All rights reserved.
//

#import "RegistFirstVC.h"
#import "Regex.h"
#import "JKCountDownButton.h"
#import "NSString+Ext.h"
#import "RegistSecondVC.h"

@interface RegistFirstVC ()

@property (nonatomic ,weak) IBOutlet JKCountDownButton *cerBtn;

@property (nonatomic ,weak) IBOutlet UITextField *phoneTf;

@property (nonatomic ,weak) IBOutlet UITextField *cerTf;


@end

@implementation RegistFirstVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = self.pwdType == ForgotPwdType?@"忘记密码":@"注册第一步";

    @WeakObj(self);
    self.cerBtn.didUpdateBlock = ^(NSString *title){
        @StrongObj(self);
        
    };
    [self.cerBtn addToucheHandler:^(NSInteger tag) {
        @StrongObj(self);
        Strongself.cerBtn.enabled = NO;
        [Strongself.view endEditing:YES];
        
        if ([Strongself.phoneTf.text length] > 0) {
            if (![Regex isValidatePhoneNumber:Strongself.phoneTf.text]) {
                
                Strongself.cerBtn.enabled = YES;

                [Strongself.view makeToast:@"请输入正确的手机号码"];
            }else{
                
                NSDictionary *params = @{
                                         @"msisdn":Strongself.phoneTf.text
                                         };
                [MHNetworkManager postReqeustWithURL:@"" params:params successBlock:^(NSDictionary *returnData) {

                    XLBaseModel *data = [XLBaseModel mj_objectWithKeyValues:returnData];

                    if (data.resultFlag) {
                        
                        Strongself.cerTf.text = [[[returnData mj_JSONObject] objectForKey:@"data"] objectForKey:@"ver_code"];
                        [Strongself.view makeToast:@"短信密码发送成功"];
                        
                        [Strongself.cerBtn startWithSecond:10];
                        Strongself.cerBtn.userInteractionEnabled = NO;
                        [Strongself.cerBtn didChange:^NSString *(int second) {
                            NSString *title = [NSString stringWithFormat:@"已发送(%d)",second];
                            return title;
                        }];
                        [Strongself.cerBtn didFinished:^NSString *(int second) {
                            
                            Strongself.cerBtn.enabled = YES;
                            Strongself.cerBtn.userInteractionEnabled = YES;
                            
                            return @"发送验证码";
                        }];
                        
                        Strongself.cerTf.text = @"";
                        
                        
                    }else{

                        [Strongself.view makeToast:@"短信密码发送失败"];

                    }

                } failureBlock:^(NSError *error) {

                    [Strongself.view makeToast:@"短信密码发送失败"];

                } showHUD:YES];
                
            }
        }else{
            
            Strongself.cerBtn.enabled = YES;

            [Strongself.view makeToast:@"请输入手机号码"];
        }
        
    }];
    
}

- (void)forgotPwdAction:(UIButton *)btn
{
    RegistFirstVC * vc = [[RegistFirstVC alloc] initWithNibName:@"RegistFirstVC" bundle:nil];
    vc.pwdType = ForgotPwdType;
    [self showViewController:vc sender:nil];
}



- (BOOL)regexRegistInfo
{
    if ([[NSString deleteForeAndAftSpace:self.phoneTf.text] length] == 0) {
        
        [self.view makeToast:@"请输入手机号码"];
        return NO;
        
    }else if (![Regex isValidatePhoneNumber:self.phoneTf.text]){
        
        [self.view makeToast:@"请输入正确的手机号码"];
        return NO;
        
    }else if ([[NSString deleteForeAndAftSpace:self.cerTf.text] length] == 0){
        
        [self.view makeToast:@"请输入手机验证码"];
        return NO;
    }
    
    return YES;
    
}

- (IBAction)nextAction:(id)sender
{
    [self.view endEditing:YES];
    if (![self regexRegistInfo]) {
        return;
    }
    
//    NSDictionary *params = @{
//                             @"type": @"OSWIFI_LOGIN_MOBILE",
//                             @"requestEntity": @{
//                                     @"telephone": self.phoneTf.text,
//                                     @"identifyingCode":self.cerTf.text
//                                     }
//                             };
//    @WeakObj(self);
//    [MHNetworkManager postReqeustWithURL:nil params:params successBlock:^(NSDictionary *returnData) {
//        XLBaseModel *data = [XLBaseModel mj_objectWithKeyValues:returnData];
    
    RegistSecondVC *secondVC = [[RegistSecondVC alloc] initWithNibName:@"RegistSecondVC" bundle:nil];
    secondVC.pwdType = self.pwdType;
    [self showViewController:secondVC sender:nil];
//
//    } failureBlock:^(NSError *error) {
//
//        [Weakself.view makeToast:@"登录失败"];
//
//    } showHUD:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
